package com.wiz.netty.service;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 * @author Hakeny
 */
public class NettyThreadPool {
//	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	// 池中所保存的线程数，包括空闲线程。执行线程数及最大线程数，必须远远小于数据库活动连接数，不然易造成死锁()
	private final static int COREPOOLSIZE = 20;
	// 池中允许最大线程数，活动线程数<2的情况下，死锁基本消除，但是依然存在可能性
	private final static int MAXPOOLSIZE = 100;
	// 当线程数大于核心时，此为终止前多余的空闲线程等待新任务的最长时间
	private final static int ALIVETIME = 3;
	// 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。
	private final static LinkedBlockingQueue<Runnable> WORKQUEUE = new LinkedBlockingQueue<Runnable>();
//	private final static ArrayBlockingQueue<Runnable> WORKQUEUE = new ArrayBlockingQueue<Runnable>(50000);

	/**
	 * 池化
	 */
	private static ThreadPoolExecutor nettyThreadPool = new ThreadPoolExecutor(
			COREPOOLSIZE, MAXPOOLSIZE, ALIVETIME, TimeUnit.SECONDS, WORKQUEUE,
			new PoolExceptionHandler());

	/**
	 * 执行任务
	 * @param t
	 */
	public static void proccess(Runnable t) {
		nettyThreadPool.execute(t);
	}
	
}
